<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="部门/公司名称：" prop="deptName">
          <el-col :span="12">
            <el-input v-model="form.deptName" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="上级部门/公司：">
          <el-col :span="12">
            <el-select v-model="form.parentId" placeholder="请选择" clearable>
              <el-option
                v-for="item in deptOptions"
                :key="item.id"
                :label="item.deptName"
                :value="item.id"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="属性：" prop="deptType">
          <el-col :span="12">
            <el-radio-group v-model="form.deptType">
              <el-radio :label="0">部门</el-radio>
              <el-radio :label="1">公司</el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>
        <el-form-item label="联系人：">
          <el-col :span="12">
            <el-input v-model="form.email" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="联系人手机号：">
          <el-col :span="12">
            <el-input v-model="form.mobile" placeholder="请输入" maxlength="11" clearable />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDept, editDept, getDeptDetail, getDeptList } from '@/api/system'
export default {
  name: 'AddDepart',
  props: {
    type: {
      type: String,
      default: 'add' // add:新增 edit:编辑
    },
    id: {
      type: [String, Number],
      default: ''
    }
  },

  data() {
    return {
      loading: false,
      deptOptions: [],
      form: {
        deptName: '',
        parentId: '',
        deptType: '',
        email: '',
        mobile: ''
      },
      rules: {
        deptName: [{ required: true, message: '请输入部门/公司名称', trigger: ['blur'] }],
        deptType: [{ required: true, message: '请选择属性', trigger: ['change'] }]
      }
    }
  },

  computed: {
    title() {
      if (this.type === 'add') {
        return '新增部门/公司'
      } else {
        return '编辑部门/公司'
      }
    }
  },

  created() {
    this.getDeptList()
    if (this.type === 'edit') {
      this.getDetail()
    }
  },

  methods: {
    // 获取部门列表
    getDeptList() {
      const data = {
        nested: false
      }
      getDeptList(data).then(res => {
        this.deptOptions = res || []
      }).catch(() => {})
    },

    // 获取详情
    getDetail() {
      const data = {
        id: this.id
      }
      getDeptDetail(data).then(res => {
        const { deptName, deptType, email, mobile, parentDept } = res
        const parentId = parentDept?.id
        this.form = {
          deptName,
          parentId,
          deptType,
          email,
          mobile
        }
      }).catch(() => {
      })
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      if (this.type === 'add') {
        this.addHandle()
      } else {
        this.editHandle()
      }
    },

    // 新增
    addHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form
          }
          addDept(data).then(() => {
            this.$message.success('新增成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    // 编辑
    editHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.id
          }
          editDept(data).then(() => {
            this.$message.success('编辑成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
</style>
